package top.hcode.hoj.service.admin.system.impl;


import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import top.hcode.hoj.mapper.ConfigurationMapper;
import top.hcode.hoj.pojo.vo.ConfigurationVO;
import top.hcode.hoj.service.admin.system.ConfigurationService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Configuration)表服务实现类
 *
 * @author guozhao
 * @since 2025-04-23 09:26:38
 */
@Service("configurationService")
public class ConfigurationServiceImpl implements ConfigurationService {
    @Resource
    private ConfigurationMapper configurationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ConfigurationVO queryById(Integer id) {
        return this.configurationMapper.queryById(id);
    }
    /**
     * 通过code查询单条数据
     *
     * @param code 配置编码
     * @return 实例对象
     */
    @Override
    public ConfigurationVO queryByCode(String code) {
        return this.configurationMapper.queryByCode(code);
    }

    /**
     * 分页查询
     *
     * @param configuration 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ConfigurationVO> queryByPage(ConfigurationVO configuration, PageRequest pageRequest) {
        long total = this.configurationMapper.count(configuration);
        return new PageImpl<>(this.configurationMapper.queryAllByLimit(configuration, pageRequest), pageRequest, total);
    }

    //查询所有
    @Override
    public List<ConfigurationVO> quaryAll(ConfigurationVO configuration){
        return configurationMapper.queryAll(configuration);
    }


    /**
     * 新增数据
     *
     * @param configuration 实例对象
     * @return 实例对象
     */
    @Override
    public ConfigurationVO insert(ConfigurationVO configuration) {
        this.configurationMapper.insert(configuration);
        return configuration;
    }

    /**
     * 批量插入操作
     * @param configList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<ConfigurationVO> configList) {
        if (configList == null || configList.isEmpty()) {
            return 0;
        }
        return configurationMapper.insertBatch(configList);
    }

    /**
     * 修改数据
     *
     * @param configuration 实例对象
     * @return 实例对象
     */
    @Override
    public ConfigurationVO update(ConfigurationVO configuration) {
        this.configurationMapper.update(configuration);
        return this.queryById(configuration.getId());
    }

    /**
     * 通过code修改数据
     *
     * @param configuration 实例对象
     * @return 实例对象
     */
    @Override
    public ConfigurationVO updateByCode(ConfigurationVO configuration) {
        this.configurationMapper.updateByCode(configuration);
        return this.queryById(configuration.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.configurationMapper.deleteById(id) > 0;
    }
}
